package org.study.plus.boot.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 参考{@link PageDTO}，把其中的Long改为Integer，解决SpringBoot全局把Long转成String后，前端分页失效的问题
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2023年12月07日
 */
@Data
public class PageVO<T> implements Serializable {
    private static final long serialVersionUID = 259557573894230639L;

    // private List<OrderItem> orders;
    private List<T> records;
    private int current;
    private int size;
    private int total;
    private int pages;

    public PageVO() {
        this(1, 10);
    }

    public PageVO(long current, long size) {
        this(current, size, 0);
    }

    public PageVO(long current, long size, long total) {
        this(current, size, total, 0);
    }

    public PageVO(long current, long size, long total, long pages) {
        this(Collections.emptyList(), current, size, total, pages);
    }

    public PageVO(List<T> records, long current, long size, long total, long pages) {
        // this.orders = Collections.emptyList();
        this.records = records;
        this.current = (int) current;
        this.size = (int) size;
        this.total = (int) total;
        this.pages = (int) pages;
    }

    public static <T> PageVO<T> of(long current, long size) {
        return of(current, size, 0L);
    }

    public static <T> PageVO<T> of(long current, long size, long total) {
        return of(current, size, total, 0L);
    }

    public static <T> PageVO<T> of(long current, long size, long total, long pages) {
        return of(Collections.emptyList(), current, size, total, pages);
    }

    public static <T> PageVO<T> of(List<T> records, long current, long size, long total, long pages) {
        return new PageVO(records, current, size, total, pages);
    }

    public static <T> PageVO<T> of(IPage<T> iPage) {
        return of(iPage.getRecords(), iPage.getCurrent(), iPage.getSize(), iPage.getTotal(), iPage.getPages());
    }
}
